package com.D5Power.Editor
{
	import com.D5Power.Controler.NCharacterControler;
	import com.D5Power.Controler.Perception;
	import com.D5Power.Editor.Objects.NCharacterObject_MaEd;
	import com.D5Power.Objects.BuildingObject;
	import com.D5Power.Objects.Effects.RoadPoint;
	import com.D5Power.Objects.GameObject;
	import com.D5Power.Objects.NCharacterObject;
	import com.D5Power.Render.RenderNCharacter;
	import com.D5Power.Render.RenderStatic;
	import com.D5Power.graphicsManager.GraphicsResource;
	import com.D5Power.basic.ResourcePool;
	import com.D5Power.ns.NSCamera;
	import com.D5Power.scene.D5Scene;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.geom.Point;
	
	use namespace NSCamera;
	
	public class EditorScene extends D5Scene
	{
		/**
		 * 脚本
		 */
		public var lua_file:String='';
		/**
		 * 等待添加的游戏对象
		 */ 
		private var _waitInsert:GameObject;
		
		public function EditorScene(stg:Stage,container:DisplayObjectContainer)
		{
			super(stg,container);
			setMap();
			
			perc = new Perception(this);
			Global.resPool = new ResourcePool();
			
			_isReady=true;
		}
		
		/**
		 * 创建建筑
		 */ 
		public function createEditorBuilding(s:String,pos:Point=null,name:String=''):BuildingObject
		{
			var res:GraphicsResource = new GraphicsResource(null,name);

			res.addLoadResource(['asset/map/map'+D5Power.mapid+'/'+s]);
			
			var house:BuildingObject = new BuildingObject(this);
			if(pos!=null)
			{
				house.setPos(pos.x,pos.y);
				
			}
			house.render = render_building;
			house.graphicsRes = res;
			house.zero = new Point();
			
			if(pos==null)
			{
				house.setPos(Map.Center.x+50,Map.Center.y+50);
				_waitInsert = house;
			}
			addObject(house);
			
			return house;
		}
		
		/**
		 * 取消创建
		 */ 
		public function cancleCreateObject():void
		{
			if(_waitInsert==null) return;
			removeObject(_waitInsert);
			_waitInsert=null;
		}
		
		/**
		 * 确认待创建
		 */ 
		public function fallObject(pos:Point):void
		{
			if(_waitInsert==null) return;

			_waitInsert.setPos(pos.x,pos.y);
			_waitInsert=null;
		}
		
		/**
		 * 创建NPC
		 * @param	s		位图资源
		 * @param	name	资源名称
		 */ 
		override public function createNPC(s:String,resname:String,name:String='NPC',pos:Point=null,dirConfig:Object=null):NCharacterObject
		{
			var res:GraphicsResource;
			if(dirConfig==null)
			{
				res = new GraphicsResource(null,resname);
			}else{
				res = new GraphicsResource(null,resname,dirConfig.frameTotal,dirConfig.frameLayer,dirConfig.fps,dirConfig.mirror);
			}

			res.addLoadResource(['asset/map/map'+Map.mapid+'/npc/'+s]);
			
			var npc:NCharacterObject_MaEd = new NCharacterObject_MaEd(new NCharacterControler(perc));
			npc.render = render_npc;
			npc.graphicsRes=res;
			npc.setName(name);
			
			if(pos!=null)
			{
				npc.setPos(pos.x,pos.y);
			}else{
				npc.setPos(Map.Center.x+50,Map.Center.y+50);
				_waitInsert = npc;
			}
			addObject(npc);
			
			return npc;
		}
		
		override public function createRoad(posx:uint = 0,posy:uint = 0):RoadPoint
		{
			var road:RoadPoint = super.createRoad(posx,posy);
			
			if(posx!=0)
			{
				road.setPos(posx,posy);
			}else{
				_waitInsert = road;
				road.setPos(Map.Center.x+50,Map.Center.y+50);
			}
			
			return road;
		}
		
		
		/**
		 * 获取待创建对象
		 */ 
		public function get waitInsertObject():GameObject
		{
			return _waitInsert;
		}
		
		/**
		 * 设置准备状态为OK
		 */ 
		private function startRander(e:Event):void
		{
			//trace(3);
			_isReady=true;
		}
		
		
		
		
	}
}